package com.ruoyi.hr.controller.hr.welfare.welfarecalculation;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.domain.salaryCalculation.SkypayGroups;
import com.ruoyi.hr.domain.welfare.welfarecalculationconfig.CcdBenefititem;
import com.ruoyi.hr.service.AuthorityService;
import com.ruoyi.hr.service.EmpSalaryCalculationService;
import com.ruoyi.hr.service.welfare.welfarecalculation.WelfareCalculationService;
import com.ruoyi.hr.service.welfare.welfarecalculationconfig.ICcdBenefititemService;
import com.ruoyi.hr.util.BaseResult;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 福利计算
 *
 * @author zeo
 * @date 2020-1-9
 */
@Controller
@RequestMapping("/welfare/calculation")
public class WelfareCalculationController extends BaseController {
    @Autowired
    private EmpSalaryCalculationService empSalaryCalculationService;

    @Autowired
    private WelfareCalculationService welfareCalculationService;

    @Autowired
    ICcdBenefititemService iCcdBenefititemService;

    @Autowired
    ISysUserService iSysUserService;

    @Autowired
    AuthorityService authorityService;

    private String prefix = "hr/welfare/welfarecalculation/welfarecalculation";

    @GetMapping()
    public String calculation() {
        return prefix + "/welfarecalCulation";
    }

    /**
     * 获取薪资套列表
     *
     * @return 薪资套列表集合
     */
    @PostMapping("/getSkypayGroups")
    @ResponseBody
    public List<SkypayGroups> getSkypayGroups() {
        SysUser user = ShiroUtils.getSysUser();
        Map<String, Object> maps = iSysUserService.selectSkySecUser(user.getLoginName());
        return empSalaryCalculationService.findSkypayGroups(maps.get("id").toString());
    }

    /**
     * 获取福利过程
     *
     * @return
     */
    @PostMapping("/findWelfareProcess")
    @ResponseBody
    public TableDataInfo findWelfareProcess() {
        List<Map<String, Object>> list = welfareCalculationService.findWelfareProcess();
        return getDataTable(list);
    }

    /**
     * 初始化 计算 冻结 取消冻结 封账
     */
    @PostMapping("/procedure")
    @ResponseBody
    public BaseResult procedure(String allid, String procedure) {
        return welfareCalculationService.executeProcedure(allid, procedure);
    }

    /**
     * 获取人员福利数据
     *
     * @param pgid
     * @return
     */
    @PostMapping("/findEmpWelfareData")
    @ResponseBody
    public TableDataInfo findEmpWelfareData(String pgid, @RequestParam Map<String, String> map) {
        SysUser user = ShiroUtils.getSysUser();
        List<Authority> authorityList = authorityService.getAuthorityUser(user.getUserId().toString());
        int isAdmin = 0;
        if (authorityList.size() == 1) {
            if (authorityList.get(0).getIsAdmin() != null) {
                isAdmin = 1;
            }
        }
        startPage();
        List<Map<String, Object>> list = welfareCalculationService.findEmpWelfareData(pgid,map,authorityList,isAdmin);
        return getDataTable(list);
    }

    /**
     * 动态获取表头
     */
    @PostMapping("/findHeader")
    @ResponseBody
    public List<CcdBenefititem> findHeader() {
        List<CcdBenefititem> ccdBenefititems = iCcdBenefititemService.findAlls();
        return ccdBenefititems;
    }
    /**
     * 新增跳转
     *
     * @return
     */
    @GetMapping("/process")
    public String process() {
        return prefix + "/process";
    }
}
